﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Comp376A2_9609695
{
    /// <summary>
    /// The Pooka monster
    /// </summary>
    class Pooka : Monster, Drawable
    {
        public SpriteSheet spritesheet;
        private String animName;
        private List<int> animFrames;
        private int frame;
        private int frameTime;
        private int frameLength;
        public String animation
        {
            get { return animName; }
            set
            {
                if (spritesheet.getAnimList().Contains(value))
                {
                    if (animName != value)
                    {
                        animName = value;
                        animFrames = spritesheet.getAnimation(animName);
                        frame = 0;
                        frameTime = 0;
                    }
                }
            }
        }

        public Pooka(int startX, int startY) : base (startX, startY)
        {
            frameLength = 200;
        }

        public override void update(GameTime time, GameController control)
        {
            base.update(time, control);
            speed = 6 + control.currLevel * 2;
            // If the Pooka is being pumped, set the frame
            if (pumpLevel != 0)
            {
                animation = "pump";
                frame = 0;
                if (pumpLevel > 1)
                    frame = 1;
                if (pumpLevel > 3)
                    frame = 2;
                if (pumpLevel > 5)
                    frame = 3;
            }
            else if (ghosting)
                animation = "ghost";
            else
                animation = "walk";
            frameTime += elapsed;
            if (frameTime >= frameLength && pumpLevel == 0)
            {
                frame = (frame + 1) % animFrames.Count;
                frameTime -= frameLength;
            }
        }

        public override void reset()
        {
            base.reset();
            animation = "walk";
        }

        Texture2D Drawable.getImage()
        {
            return spritesheet.image;
        }

        void Drawable.draw(SpriteBatch renderer)
        {
            Rectangle sprite = spritesheet.getSprite(animFrames.ElementAt(frame));
            // If the pooka is squished, the sprite is squished
            if (squished)
            {
                Rectangle drawRect = new Rectangle((int)position.X, (int)position.Y, sprite.Width, sprite.Height);
                drawRect.Y += sprite.Height - 4;
                drawRect.Height = 4;
                renderer.Draw(spritesheet.image, drawRect, sprite, Color.White);
            }
            else
            {
                Vector2 drawPos = new Vector2(position.X, position.Y);
                if (pumpLevel > 0)
                    drawPos.Y -= 5;
                if (facing == Direction.Right)
                {
                    renderer.Draw(spritesheet.image, drawPos, sprite, Color.White, 0f, new Vector2(), 1f, SpriteEffects.FlipHorizontally, 0);
                }
                else
                    renderer.Draw(spritesheet.image, drawPos, sprite, Color.White);
            }
        }

        public override bool collides(Rectangle box, bool pumping = false)
        {
            if ((ghosting || pumpLevel != 0) && !pumping)
                return false;
            else
            {
                Rectangle bounds = new Rectangle((int)position.X + 2, (int)position.Y + 3, 13, 12);
                Rectangle intersection = new Rectangle();
                Rectangle.Intersect(ref box, ref bounds, out intersection);
                return (intersection.Width != 0 && intersection.Height != 0);
            }
        }
    }
}
